package com.dhnsoft.代码随想录.字符串;

/**
 * ClassName: d02_541_反转字符串Ⅱ
 * Package: com.dhnsoft.代码随想录.字符串
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/18 11:07
 * @Version 1.0
 */
public class d02_541_反转字符串Ⅱ {
    public static void main(String[] args) {
        String abcdefg = reverseStr("jlnnxsetgcpsbhsfymrkhfursyissjnsocgdhgfxtxrlvugsaphqzxllwebukgatzfybprfmmfithphckksnvjkcvnsqgsgosfxc", 20);
        System.out.println(abcdefg);
    }



    public String reverseString(String s,int k){
        StringBuilder res = new StringBuilder();
        int length = s.length();
        int start = 0;
        while (start<length){
            //找到字符串的k处2k处临时存放在temp中
            StringBuilder temp = new StringBuilder();
            //获取首个节点的长度，若大于length则取length,这部分是必须反转的
            int firstK = (start+k>length)?length:start+k;
            //取第2k个节点长度，若这个长度大于length则取length，小于length时k到2k的距离不反转
            int secondK = (start+2*k>length)?length:start+2*k;

            //将前k个节点放入到temp中
            temp.append(s.substring(start,firstK));
            //再将temp中的值反转
            res.append(temp.reverse());

            //若firstK小于secondK则将剩余部分放入到res中
            if(firstK<secondK){
                res.append(s.substring(firstK,secondK));
            }
            start+=2*k;
        }
        return res.toString();
    }

    public static String reverseStr(String s, int k) {
        char[] chars = s.toCharArray();
        if(chars.length<k){
            char[] charsResult = reverseString(chars);
            return String.valueOf(charsResult);
        }
        char[] newChars;
        if(chars.length>=k&&chars.length<2*k){
            newChars = new char[k];
            for (int i=0;i<k;i++){
                newChars[i] = chars[i];
            }
            char[] charsHalf = reverseString(newChars);
            for (int i=0;i<k;i++){
                chars[i] = charsHalf[i];
            }
            return String.valueOf(chars);
        }
        int j = 2*k;
        int q = k;
        while (j<=chars.length){
            for (int i=j-2*k,z=q-1;i<z;i++,z--){
                char temp = chars[i];
                chars[i] = chars[z];
                chars[z] = temp;
            }
            q +=2*k;
            j+=2*k;
        }
        j-=2*k;
        if(chars.length==j){
            return String.valueOf(chars);
        }

        if(chars.length-j<k){
            for (int i=j,z=chars.length-1;i<z;i++,z--){
                char temp = chars[i];
                chars[i] = chars[z];
                chars[z] = temp;
            }
            return String.valueOf(chars);
        }
        if(chars.length-j<2*k&&chars.length-j>k){
            for (int i=j,z=chars.length-k;i<z;i++,z--){
                char temp = chars[i];
                chars[i] = chars[z];
                chars[z] = temp;
            }
            return String.valueOf(chars);
        }
        if(chars.length-j<2*k&&chars.length-j==k){
            for (int i=j,z=chars.length-1;i<z;i++,z--){
                char temp = chars[i];
                chars[i] = chars[z];
                chars[z] = temp;
            }
            return String.valueOf(chars);
        }
        return String.valueOf(chars);

    }

    public static char[] reverseString(char[] s) {
        int i=0;
        int j=s.length-1;
        while (i<j){
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            i++;
            j--;
        }
        return s;
    }

}
